package com.qf.forumproject.loginAndRegister.controller;

import com.qf.forumproject.entity.QfUser;
import com.qf.forumproject.utils.RegisterVo;
import com.qf.forumproject.utils.Result;
import com.qf.forumproject.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.qf.forumproject.loginAndRegister.service.QfUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * (QfUser)控制层
 *
 * @author protagonist
 * @since 2022-07-26 08:52:44
 */
@RestController
@Slf4j
@RequestMapping("/qfUser")
@Api(tags = "用户模块接口")
public class QfUserController {
    /**
     * 服务对象
     */
    @Resource
    private QfUserService qfUserServiceImpl;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @param rememberMe
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("用户登录接口")
    public Result login(String username, String password, boolean rememberMe) {

        log.debug("入参，username={},password={},rememberMe={}", username, password, rememberMe);

        final Subject subject = SecurityUtils.getSubject();

        //5、判断用户是否登录过
        if (!subject.isAuthenticated()) {
            subject.login(new UsernamePasswordToken(username, password, rememberMe));
            QfUser qfUser = (QfUser) subject.getPrincipal();

            if (Objects.nonNull(qfUser)){
                return new Result<>(true, StatusCode.OK, "用户登录成功", qfUser);
            }
            return new Result<>(true, StatusCode.ERROR, "用户登录失败");
        }
        return new Result<>(true, StatusCode.ERROR, "该用户未进行登录验证");
    }


    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation("退出登录接口")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        return new Result<>(true, StatusCode.OK, "退出成功");
    }

    /**
     * 用户注册
     *
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册接口")
    public Result register(RegisterVo registerVo) {

        QfUser user = qfUserServiceImpl.insert(registerVo);

        if (Objects.nonNull(user)) {
            return new Result<>(true, StatusCode.OK, "用户新增成功", user);
        }
        return new Result<>(true, StatusCode.ERROR, "用户新增失败");
    }


    //获取用户头像
    @GetMapping("/getAvatar")
    @ApiOperation("获取用户头像接口")
    public void getAvatar(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取当前用户对象
        Subject subject = SecurityUtils.getSubject();
        QfUser user = (QfUser) subject.getPrincipal();
        if (Objects.nonNull(user)) {
            IOUtils.copy(new FileInputStream(user.getAvatar()),resp.getOutputStream());
        }
    }
}
